<?php

declare(strict_types=1);

namespace App\AdminApi\Controller;

use App\AdminApi\BasicsController;
use App\Common\Extend\Guard\Auth;
use Core\HyperfUnit\Response;
use Core\HyperfUnit\TheServer;
use Core\Package\Attribute\Describe;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Psr\Http\Message\ResponseInterface;

#[
    Controller(prefix: '/admin-api', server: self::SERVER),
    Auth(guard: self::GUARD, app: self::APP)
]
class IndexController extends BasicsController
{
    #[
        Describe('获取后台配置'),
        GetMapping('configure')
    ]
    public function configure(): ResponseInterface
    {
        // 后台配置
        $configure = configure('admin');
        $configure['debug'] = IsDebug();

        // 腾讯地图
        $configure['map'] = configure('map');

        // 公司名称
        $configure['company'] = configure('system.company');

        $configure['environment'] = ServerOs();

        $configure['host'] = configure('system.host');

        $configure['mourn'] = (int)configure('system.mourn');

        return Response::Api()->success(data: $configure);
    }

    #[
        GetMapping('system-info'),
    ]
    public function SystemInfo(TheServer $server): ResponseInterface
    {
        $composer = $server->composerInfo();
        $os = ServerOs();
        $result['program'] = [
            'os' => "[{$os['type']}] {$os['name']}",
            'swoole_version' => SWOOLE_VERSION,
            'php_version' => PHP_VERSION,
            'hyperf_version' => $composer['hyperf/framework'] ?? '?',
            'mysql_version' => Db::selectOne('SELECT @@version as `version`')->version ?? '',
        ];
        $result['run'] = time() - RUN_TIME;
        return Response::Api()->success(data: $result);
    }

    #[
        GetMapping('logger-size')
    ]
    public function loggerSize(TheServer $server): ResponseInterface
    {
        $size = $server->loggerSize();
        return Response::Api()->success(data: $size);
    }
}
